from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

# 打开浏览器
driver = webdriver.Firefox()
time.sleep(2)
# 设置窗口大小
driver.set_window_size(400, 800)
time.sleep(2)
# 双击打开百度页面
driver.get('https://www.baidu.com/')
time.sleep(2)
# 窗口最大化
driver.maximize_window()
time.sleep(2)
time.sleep(2)
seatch = driver.find_element(By.ID, 'kw')
# 键盘输入事件
seatch.send_keys('selenium键盘鼠标浏览器事件')
time.sleep(2)
# 键盘回车事件
seatch.send_keys(Keys.ENTER)
time.sleep(2)
# 浏览器：返回
driver.back()
time.sleep(2)
# 浏览器：前进
driver.forward()
time.sleep(2)
# 键盘事件
# 回车事件
seatch.send_keys(Keys.ENTER)
# 键盘Ctrl事件  全选 | 复制 | 粘贴 | 剪切
seatch.send_keys(Keys.CONTROL, 'a')
seatch.send_keys(Keys.CONTROL, 'c')
seatch.send_keys(Keys.CONTROL, 'v')
seatch.send_keys(Keys.CONTROL, 'x')
# 键盘shift事件
seatch.send_keys(Keys.SHIFT, 'F1')
seatch.send_keys(Keys.SHIFT, 'F12')
# 键盘tab事件
seatch.send_keys(Keys.TAB)
# 键盘space事件  空格
seatch.send_keys(Keys.SPACE)
# 删除事件
seatch.send_keys(Keys.DELETE)
seatch.send_keys(Keys.BACKSPACE)
# 键盘 上 下 左 右  事件
seatch.send_keys(Keys.UP)
seatch.send_keys(Keys.DOWN)
seatch.send_keys(Keys.LEFT)
seatch.send_keys(Keys.RIGHT)


# 鼠标事件
# 悬停事件
ActionChains(driver).move_to_element(seatch).perform()
# 点击事件
ActionChains(driver).click(seatch).perform()
# 右击事件
ActionChains(driver).context_click(seatch).perform()
# 双击事件
ActionChains(driver).double_click(seatch).perform()
# 三击事件
ActionChains(driver).double_click(seatch).click(seatch).perform()
# 拖拽事件
ActionChains(driver).drag_and_drop(seatch, seatch).perform()
ActionChains(driver).drag_and_drop_by_offset(seatch, 50, 40).perform()
# 长按事件
ActionChains(driver).click_and_hold(seatch).perform()
# 鼠标释放事件
ActionChains(driver).release(seatch).perform()
# 移动事件（按照轴像素移动）
ActionChains(driver).move_by_offset(30, 50).perform()

# 浏览器事件
print(f'浏览器名称：{driver.name}，浏览器尺寸：{driver.get_window_size()}, 浏览器当前URL：{driver.current_url}，浏览器标题：{driver.title}')
# 刷新
driver.refresh()
# 窗口最大化
driver.maximize_window()
# 窗口最小化
driver.minimize_window()
# 返回
driver.back()
# 前进
driver.forward()
# 打开网址
driver.get('wagnzhi')
#  关闭范围：仅当前窗口   资源释放：不释放驱动进程   后续操作：可继续操作其他窗口
driver.close()
# 关闭范围：所有窗口 + 驱动进程   资源释放：完全释放驱动进程   后续操作：需重新初始化 driver 才能操作
driver.quit()

time.sleep(10)
driver.quit()



